define(function(require, exports, module) {

	var $ = require('jquery');
	var List = require('../util/list');
	require('artTemplate/artTemplate/2.0.1/template.js');
	require('seajs/seajs/2.1.1/sea.js');
	require('jquery/jquery-form/1.0.0/jquery.form.js');
	require('jquery/jquery-validation/1.11.0/jquery.validate');
	require('jquery/jquery-validation/1.11.0/messages_zh');
	require('jbox');
	
	exports.options = {
		listLink : '',
		listParams : '',
		pageLink : '',
		pageParams : ''
	};
	
	var pro_id = 0;
	var by = '';
	var value = '';
	var all = 1;
	// 时间戳转换成日期
	exports.getLocalTime = function(t, callback) {
		var now = new Date(parseInt(t));// 转化long型日期为时间对象
		var year = now.getFullYear();
		var month = now.getMonth() + 1;
		var date = now.getDate();
		var hour = now.getHours();
		var minute = now.getMinutes();
		var second = now.getSeconds();
		var newDate = year + "-" + month + "-" + date + " " + hour + ":"
				+ minute + ":" + second;
		
		if (typeof (callback) == "function") { callback(newDate); }
		 
		return newDate;
	};
	
	//所有优惠券列表
	exports.showAllCouponList = function showAllCouponList(e,pro_id) {
		var couponList = new List();
		couponList.options.template = "couponList-template";
		couponList.options.container = "#couponList";
		couponList.options.links = { "list" : $CONFIG.base_url + "/api/project/income/coupon/listall" };
		couponList.options.params = {"p" : e,"s":10,"projectId" : pro_id};
		couponList.load(function(){
			$(".coupontime").each(function() {
				var t = $(this).text();
				exports.getLocalTime(t);
			});
			$(".couponstatus").each(function() {
				var st = $.trim($(this).text());
				if (st == 0) {
					$(this).text("未使用");
				} else if (st == 1) {
					$(this).text("已使用");
				} else if(st == 2) {
					$(this).text("已作废");
				}
			});
			$(".cou-label").each(function() {
				var val =$.trim($(this).text());
				if (val == "goods") {
					$(this).text("实物券");
				} else if (val == "money") {
					$(this).text("现金券");
				} else if (val == "activity") {
					$(this).text("虚拟券");
				}
			});
			
		});
		
	};
	
	
	//各个类型和状态的优惠券筛选公用方法
	exports.showCouponList = function showCouponList(by,value,pro_id) {
		var couponList = new List();
		couponList.options.template = "couponList-template";
		couponList.options.container = "#couponList";
		couponList.options.links = { "list" : $CONFIG.base_url + "/api/project/income/admin/coupon/"+ by + "/" + value + "/" + "projectId"};
		couponList.options.params = {
				"p" : 1,
				"s":10,
				"projectId":pro_id
				};
		couponList.load(function(){
			
				$(".coupontime").each(function() {
					var t = $(this).text();
					exports.getLocalTime(t);
				});
				$(".couponstatus").each(function() {
					var st = $.trim($(this).text());
					if (st == 0) {
						$(this).text("未使用");
					} else if (st == 1) {
						$(this).text("已使用");
					} else if (st == 2) {
						$(this).text("已作废");
					}
				});
				$(".cou-label").each(function() {
					var val =$.trim($(this).text());
					if (val == "goods") {
						$(this).text("实物券");
					} else if (val == "money") {
						$(this).text("现金券");
					} else if (val == "activity") {
						$(this).text("虚拟券");
					}
				});
		
		});
		
	};
	
	//选择项目列表
	exports.showProjectList = function showProjectList() {
		var getting = $.get($CONFIG.base_url + "/api/project/income/projectName/list");
		getting.done(function(data) {
			$.each(data,function(key,val){
				var $option = $("<option id=" + val[0] + ">"+ val[1] + "</option>");
				$("select").append($option);
			})
		});
	};
	
	exports.showProjectList();
	//筛选所有优惠券
	$("#AllCoupon").click(function() {
		$("#couponList").empty();
		all = 1;
		exports.showAllCouponList(1,pro_id);		
	});
	
	//筛选现金券
	$("#money_coupon").click(function(){
		$("#couponList").empty();
		by = "label";
		value = "money";
		all = 0;
		exports.showCouponList(by,value,pro_id);
	});
	
	//筛选实物券
	$("#goods_coupon").click(function(){
		$("#couponList").empty();
		by = "label";
		value = "goods";
		all = 0;
		exports.showCouponList(by,value,pro_id);
	});
	
	//筛选虚拟券
	$("#activity_coupon").click(function(){
		$("#couponList").empty();
		by = "label";
		value = "activity";
		all = 0;
		exports.showCouponList(by,value,pro_id);
	});
	
	//筛选已发放
	$("#provided_coupon").click(function(){
		$("#couponList").empty();
		by = "couponStatus";
		value = "0";
		all = 0;
		exports.showCouponList(by,value,pro_id);
	});
	
	//筛选已领取
	$("#accepted_coupon").click(function(){
		$("#couponList").empty();
		by = "couponStatus";
		value = "1";
		all = 0;
		exports.showCouponList(by,value,pro_id);
	});
	
	//筛选已作废
	$("#obsolete_coupon").click(function(){
		$("#couponList").empty();
		by = "couponStatus";
		value = "2";
		all = 0;
		exports.showCouponList(by,value,pro_id);
		
	});
	
	// 点击项目筛选
	$('.sort-box select').change(function() {
		pro_id = $(this).children('option:selected').attr("id");
		if(all)
			exports.showAllCouponList(1,pro_id);
		else
			exports.showCouponList(by,value,pro_id);		
	});
	

	
	$(document).ready(function() {
		$("#AllCoupon").click();
	});
	
});